<?php
namespace Home\Model;
use  Think\Model;
class MshopModel extends Model{


   /**
     * 获得旅行社信息 
     *
     */
  public  function get_info($mshop_id) {
       $Mshop=M('Mshop');
       $field="mshop_id,mshop_name,sum_profit,sum_cash_amount";
       $where="mshop_id='$mshop_id' ";
       $info=$Mshop->field($field)->where($where)->find();  

       return $info;  
    }
  

	//佣金的累加或消减
  public function deal_profit($order_sn,$mshop_id,$amount,$shipping_fee,$profit=0,$add=0)
   {
      $k_amount=$amount-$shipping_fee;//计算佣金的营业额
      $result=false;
      $now_time=gmtime();
      $mshop_frozen_profit=M('mshop_frozen_profit');
      $DB_PREFIX=C('DB_PREFIX');    
      
     if($add)
      { 
      
         //连续2个同一个订单累加
        $where=array();
        $where['order_sn']=$order_sn;
        $where['status']=array('neq',2);
        $sel_order_sn=$mshop_frozen_profit->where($where)->getField('order_sn');

       
        if($sel_order_sn)
        {
          return false;
          exit;
        }
        $data=array();
        $data['mshop_id']=$mshop_id;
        $data['order_sn']=$order_sn;
        $data['goods_price']=$k_amount;
        $data['frozen_profit']=$profit;
        $data['ctime']=$now_time;
        $data['status']=1;
        $insert_fr=$mshop_frozen_profit->add($data);

        // $where=array();
        // $where['mshop_id']=$mshop_id;
        // $data['sum_amount'] = array('exp',"sum_amount+$amount");
        // $data['sum_profit'] = array('exp',"sum_profit+$sum_profit");
        // $result=$mshop_frozen_profit->where($where)->save($data);
      
      if($insert_fr)
      {
        $sql = " update  " . $DB_PREFIX ."mshop set sum_amount=sum_amount+$amount,sum_profit=(sum_profit+$profit) WHERE mshop_id= '$mshop_id'";
        $result==M()->execute($sql); 
      }

     }
     else
     {       
       $sql_pro="update  ".$DB_PREFIX."mshop_frozen_profit  set status=2 where status!=2 and  order_sn='$order_sn'";
       $result_pro=M()->execute($sql); 
       
       if($result_pro)
       {
         $sql = " update  " .$DB_PREFIX."mshop set  sum_amount=sum_amount-$amount,sum_profit=(sum_profit-$profit) WHERE mshop_id= '$mshop_id'";
         $result=M()->execute($sql);
       }

     }
            
         return $result;
    }

}
?>
